@extends('backend.layouts.master')

@section('after-styles-end')
    {!! Html::style('css/plugin/percentagebar/style_percentage_bar.css') !!}
@stop

@section('content')
    <div class="row">
        <div class="btn-group">
            <button type="button" class="btn btn-default" onclick="exportVotes()">
                发布
            </button>
        </div>
        <div class="btn-group">
            <button type="button" class="btn btn-default" onclick="deleteVotes()">
                删除
            </button>
        </div>
        <div class="btn-group">
            <button type="button" class="btn btn-default">
                按照总浏览数排列
            </button>
        </div>
        <div class="btn-group">
            <button type="button" class="btn btn-default">
                按照总投票数排列
            </button>
        </div>
    </div>
    <div class="row">

        {!! $votes->render() !!}
        @foreach ($votes as $vote)
            <div class="panel panel-default">
                <div class="panel-heading">
                    <div class="fa" style="font-size: larger;font-weight: bold;">
                        <input type="checkbox" onclick="selectSubject(this)" id="{{ $vote->id }}"> {{ $vote->title }}
                    </div>
                    <div>
                        <div style="display: inline;">浏览数:{{ $vote->views }}</div>
                        <div style="display: inline;">投票数:{{ $vote->total }}</div>
                        @if ($vote->views > 0)
                            <div style="display: inline;">投票/浏览:{{ round($vote->total / $vote->views, 2)}}</div>
                        @endif
                    </div>
                </div>

                <div class="panel-body">
                    @if ($vote->choices != null)
                        @foreach ($vote->choices as $choice)
                            <div class="skillbar clearfix "
                                 data-percent="{{ $vote->total > 0 ? round($choice->votes * 100/ $vote->total) : 0}}%">
                                <div class="skillbar-title" style="background: #d35400;"><span>{{$choice->title}}</span>
                                </div>
                                <div class="skillbar-bar" style="background: #e67e22;"></div>
                                <div class="skill-bar-percent">{{$vote->total > 0 ? round($choice->votes * 100/ $vote->total) : 0}}%</div>
                            </div> <!-- End Skill Bar -->
                        @endforeach
                    @endif
                </div>
            </div><!-- panel -->
        @endforeach
        {!! $votes->render() !!}

    </div><!--row-->

@endsection

@section('after-scripts-end')
    <script>
        var selectedSubjects = [];
        jQuery(document).ready(function () {
            jQuery('.skillbar').each(function () {
                jQuery(this).find('.skillbar-bar').animate({
                    width: jQuery(this).attr('data-percent')
                }, 1000);
            });
        });
        function selectSubject(checkBox) {
            var selectedIndex = selectedSubjects.indexOf(checkBox.id);
            if (checkBox.checked) {
                if (selectedIndex < 0) {
                    selectedSubjects.push(checkBox.id);
                }
            } else if (selectedIndex >= 0) {
                selectedSubjects.splice(selectedIndex, 1);
            }
        }
        function deleteVotes() {
            $.ajax({
                url: "{{ URL::to('admin/delete-votes') }}",
                type: "post",
                traditional: true,
                data: {ids: JSON.stringify(selectedSubjects)},
                dataType: "JSON",
                success: function () {
                    selectedSubjects = [];
                    $(":checkbox").attr("checked", false);
                    window.location.reload();
                },
                error: function (xhr, type) {
                    alert('操作失败')
                }
            });

        }
        function exportVotes() {
            $.ajax({
                url: "{{ URL::to('admin/export-votes') }}",
                type: "post",
                traditional: true,
                data: {ids: JSON.stringify(selectedSubjects)},
                dataType: "JSON",
                success: function () {
                    selectedSubjects = [];
                    $(":checkbox").attr("checked", false);
                    window.location.reload();
                },
                error: function (xhr, type) {
                    alert('操作失败')
                }
            });
        }
    </script>
@stop